import axios from "axios";

import {getRedirectTo} from '../util';

// 定义状态常量
const REGISTER_SUCCESS = 'register_success';
const ERROR_MSG = 'error_msg';
const LOGIN_SUCCESS = 'login_success';
const SAVE_BOSSINFO_SUCCESS = 'save_bossInfo_success';
const LOGOUT = 'logout';
const AUTHSUCC = 'authsucc';

// 定义reducer函数
// 定义state初始状态
let userState = {
    user:'',//用户名
    isRegister:false,//是否登录/注册成功
    msg:'',//错误信息
    type:'',
    avatarText:'',//头像信息
    redirectTo:''//登录或者注册成功后，跳转地址，boss和牛人需要跳转的地址不同
}
export const registerReducer = (state=userState,action) => {
    switch(action.type) {
        case REGISTER_SUCCESS://注册成功
        return {...state,isRegister:true,user:action.paylaod.user,msg:'',redirectTo:getRedirectTo({type:action.paylaod.type,avater:action.paylaod.avatarText}),type:action.paylaod.type}
        case ERROR_MSG://登录，注册失败
        return {...state,isRegister:false,user:'',pass:'',msg:action.msg}
        case LOGIN_SUCCESS://登录成功
        return {...state,isRegister:true,user:action.paylaod.user,msg:'',redirectTo:getRedirectTo({type:action.paylaod.type,avater:action.paylaod.avatarText}),type:action.paylaod.type,avatarText:action.paylaod.avatarText}
        case SAVE_BOSSINFO_SUCCESS://保存bossinfo成功
        return {...state,...action.paylaod,isRegister:true,redirectTo:getRedirectTo({type:action.paylaod.type,avater:action.paylaod.avatarText})}
        case LOGOUT://登出
        return {...state,user:'',isRegister:false,msg:'',type:'',avatarText:'',redirectTo:'/login'}
        case AUTHSUCC://获取用户信息成功
        return {...state,...action.paylaod}
        default :
        return state
    }
}

// 定义返回action函数
function registerSuccess(data) {
    delete data.pass;
    return{type:REGISTER_SUCCESS,paylaod:data}
}
function errMsg(msg) {
    return{type:ERROR_MSG,msg:msg}
}
function loginSuccess(data) {
    delete data.pass;
    return {type:LOGIN_SUCCESS,paylaod:data}
}
function saveInfoSuccess(data) {
    delete data.pass;
    return {type:SAVE_BOSSINFO_SUCCESS,paylaod:data}
}
export function authsucc(data) {
    return {type:AUTHSUCC,paylaod:data};
}

export const authInfo = (props) => {
    // 发起请求，请求用户信息
    let that = props;
    return dispatch => {
        axios.get('/user/info').then(res=>{
            console.log(res);
            if(res.status === 200) {
                if(res.data.code === 0) {//用户登录
                    console.log('用户已经登录');
                    dispatch(authsucc(res.data.data));
                }else {//用户未登录跳转登录页
                    that.props.history.push('/login');
                }
            }
        })
    }
}
// 定义注册函数，发起请求
export const register = ({user,pass,repass,type}) => {
    console.log(user,pass,repass);
    // 进行输入参数的校验
    if(!user || !pass) {
        return errMsg('请输入用户名或密码');
    }
    if(pass !== repass) {
        return errMsg('密码输入不一致，请重新输入');
    }
    return (dispatch) => {
        axios.post('/user/register',{user,pass,type}).then(res => {
            console.log(res);
            if(res.status === 200 && res.data.code === 0) {//注册成功
                dispatch(registerSuccess({user,pass,type}));
            }else {//注册失败
                dispatch(errMsg(res.data.desc));
            }
        })        
    }
}
// 定义登录函数
export const loginFunction = ({user,pass})=> {
    if(!user || !pass) {
        return errMsg('请输入用户名或密码');
    };
    return dispatch => {
        axios.post('/user/login',{user,pass}).then(res=>{
            console.log(res);
            if(res.status === 200 && res.data.code === 0) {
                // 登录成功，跳转到主页
                dispatch(loginSuccess({user,type:res.data.data.type,avatarText:res.data.data.avatarText}));
            }else {
                // 登录失败
                dispatch(errMsg(res.data.desc));
            }
        })
    }
}
// 定义boss和牛人信息保存函数{avatarText,pickerValue,money,joinPosition,positionDiscribe,companyName}
export const saveInfo = (data) => {
    console.log(data);
    for(var key in data) {
        if(!data[key]) {
            return errMsg('请输入完整信息！！！');
        }
    }
    // debugger;
    return dispatch => {
        axios.post('/user/bosssave',{...data}).then(res => {
            console.log(res);
            if(res.status === 200 && res.data.code === 0) {
                dispatch(saveInfoSuccess(data));
            }else {
                dispatch(errMsg(res.data.desc));
            }
        })
    }
    
}
// 定义注销函数
export const logout = () => {
    return (dispatch) => {
        dispatch({type:LOGOUT});
    }
}